/**
  ******************************************************************************
  * @file    main.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    31-October-2011
  * @brief   Main program
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "cmsis_os.h"
#include "main.h"
#include "threads.h"
#include "app_ethernet.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/*--------------- LCD Messages ---------------*/
#define MESSAGE1   "     STM32F417      "
#define MESSAGE2   "  STM32F-4 Series   "
#define MESSAGE3   "  SSL Server demo   "
#define MESSAGE4   "  using HW Crypto   "

/*--------------- Tasks Priority -------------*/
#define SSL_TASK_PRIO   ( configMAX_PRIORITIES - 3 )
#define DHCP_TASK_PRIO  ( tskIDLE_PRIORITY + 2 )
#define LED_TASK_PRIO   ( tskIDLE_PRIORITY + 1 )

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void System_Setup(void);

#ifdef __GNUC__
    /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
    set to 'Yes') calls __io_putchar() */
    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured to
         168 MHz, this is done through SystemInit() function which is called from
         startup file (startup_stm32f4xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32f4xx.c file
       */

    /* Setup STM32 system (USART, RNG, HASH, CRYP) and STM324xG-EVAL resources */
    System_Setup();
    printf("starting...\r\n");
#if 0

    /* Initilaize the LwIP stack */
    LwIP_Init();

#ifdef USE_DHCP
    /* Start DHCPClient */
    xTaskCreate(LwIP_DHCP_task, "DHCP", configMINIMAL_STACK_SIZE * 2, NULL, DHCP_TASK_PRIO, NULL);
#endif
    /* Start SSL Server task */
    xTaskCreate(ssl_server, "SSL", configMINIMAL_STACK_SIZE * 10, NULL, SSL_TASK_PRIO, NULL);
    mqtt_client();
    /* Start toogleLed task : Toggle LED 4 every 200ms */
    xTaskCreate(ToggleLed, "LED", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);

    /* Start the scheduler */
    vTaskStartScheduler();

#endif

#if 1
    /* Init thread */
    osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 4);
    osThreadCreate(osThread(Start), NULL);
#endif

#if 1
    osThreadDef(IOT, IOTThread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE * 8 * 4);
    osThreadCreate(osThread(IOT), NULL);
#endif

#if 0
    /* Start SSL Client task : Connect to SSL server and provide the SSL handshake protocol */
    osThreadDef(SSLClient, SSLClientThread, osPriorityHigh, 0, configMINIMAL_STACK_SIZE * 8 * 2);
    osThreadCreate(osThread(SSLClient), NULL);
#endif

#if 0
    /* Start SSL Server task : Connect to SSL server and provide the SSL handshake protocol */
    osThreadDef(SSLSrv, SSLServerThread, osPriorityHigh, 0, configMINIMAL_STACK_SIZE * 8 * 3);
    osThreadCreate(osThread(SSLSrv), NULL);
#endif /* NEVER */


    /* Start scheduler */
    osKernelStart();


    /* We should never get here as control is now taken by the scheduler */
    for (;;);
}

/**
  * @brief  Setup STM32 system (USART, RNG, HASH, CRYP) and the STM324xG-EVAL's LCD and LEDs resources.
  * @param  None
  * @retval None
  */
void System_Setup(void)
{

	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_4);

    /* Enable USART2 clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    /* Enable RNG clock source */
    RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);

    /* Enable RNG IP  */
    RNG_Cmd(ENABLE);

    /* Enable CRYP clock */
    RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_CRYP, ENABLE);

    /* Enable HASH clock */
    RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_HASH, ENABLE);

    /* USART Configuration */

    STM_EVAL_COMInit(COM1, 115200);

#ifdef USE_LCD
    /* Initialize the STM324xG-EVAL's LCD */
    STM324xG_LCD_Init();
#endif

    /* Initialize STM324xG-EVAL's LEDs */
    STM_EVAL_LEDInit(LED1);
    STM_EVAL_LEDInit(LED2);
    STM_EVAL_LEDInit(LED3);
    STM_EVAL_LEDInit(LED4);

#ifdef USE_LCD
    /* Clear the LCD */
    LCD_Clear(Blue);

    /* Set the LCD Back Color */
    LCD_SetBackColor(Blue);

    /* Set the LCD Text Color */
    LCD_SetTextColor(White);

    /* Display messages on the LCD */
    LCD_DisplayStringLine(Line0, (uint8_t*)MESSAGE1);
    LCD_DisplayStringLine(Line1, (uint8_t*)MESSAGE2);
    LCD_DisplayStringLine(Line2, (uint8_t*)MESSAGE3);
    LCD_DisplayStringLine(Line3, (uint8_t*)MESSAGE4);
#endif
}

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART */
    USART_SendData(EVAL_COM1, (uint8_t) ch);

    /* Loop until the end of transmission */
    while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
    {}

    return ch;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {}
}
#endif
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

